package yartiss.engine.simulation.scheduling.policies.fp;

import java.util.SortedSet;

import yartiss.engine.simulation.Event;
import yartiss.engine.simulation.IEnergyProfile;
import yartiss.engine.simulation.ISchedulingPolicy;
import yartiss.engine.simulation.events.EventGenerator;
import yartiss.engine.simulation.scheduling.policies.AbstractMonoProcessorSchedulingPolicy;
import yartiss.engine.simulation.scheduling.policies.tools.FPSlackEnergyManager1;
import yartiss.engine.simulation.system.ITask;
import yartiss.engine.simulation.system.ITaskSet;
import yartiss.engine.simulation.system.taskset.TaskSetFactory;

public class FPegTest extends AbstractMonoProcessorSchedulingPolicy {

	
	private final FPSlackEnergyManager1 slackEnergyManager;

	public FPegTest(FPSlackEnergyManager1 slackEnergyManager) {
		this.slackEnergyManager = slackEnergyManager;
	}
	
	@Override
	public String getPolicyName() {
		return "FPEG_TEST";
	}

	@Override
	public ITaskSet createTaskSet() {
		return TaskSetFactory.getNewInstance("priority");
	}

	@Override
	public ISchedulingPolicy newInstance() {
		return new FPegTest(new FPSlackEnergyManager1());
	}
	
	
	@Override
	public ITask chooseNextTask(ITaskSet taskSet, IEnergyProfile energyProfile, long date, EventGenerator evGen) {

		
		
		metrics.clear();

		SortedSet<ITask> activeTasks = taskSet.getActiveTasks(date);

		if (activeTasks.isEmpty()) {
			return null;
		}

		ITask first = activeTasks.first();

		long se = slackEnergyManager.getSlackEnergy(date, taskSet, energyProfile);
		metrics.put(Event.SlackEnergy, se);

		evGen.generateEvent("check_energy_state", first, date + 1, null);
		
		if (se > 0 && energyProfile.howLongCanExecute(first) > 0) {
			return first;
		}
		
		return null;
	}

}
